﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;

namespace uk.ac.soton.ses
{
    // Adapted from example given at http://msdn.microsoft.com/en-us/library/dd460690.aspx

    class HDCWatcherQueue:IProducerConsumerCollection<HDCWatcherQueueItem>
    {
        readonly object _queueLock = new object();
        ConcurrentQueue<string> _queue = null;
        ConcurrentDictionary<string, HDCWatcherQueueItem> _dictionary = null;

        public HDCWatcherQueue()
        {
            this._queue = new ConcurrentQueue<string>();
            this._dictionary = new ConcurrentDictionary<string, HDCWatcherQueueItem>();
        }

        public void CopyTo(HDCWatcherQueueItem[] array, int index)
        {
            this._dictionary.Values.CopyTo(array, index);
        }

        public HDCWatcherQueueItem[] ToArray()
        {
            return this._dictionary.Values.ToArray();
        }

        public bool TryAdd(HDCWatcherQueueItem item)
        {
            bool added = false;
            lock(this._queueLock)
            {
                if (!this._dictionary.ContainsKey(item.Key))
                {
                    this._dictionary[item.Key] = item;
                    this._queue.Enqueue(item.Key);
                    added = true;
                }
            }
            return added;
        }

        public bool TryTake(out HDCWatcherQueueItem item)
        {
            bool success = false;

            //We have to assign something to item, whether we are successful or not
            item = null;
            lock (this._queueLock)
            {
                string itemKey;
                if(this._queue.TryDequeue(out itemKey))
                {
                    success = this._dictionary.TryRemove(itemKey, out item);
                }
            }

            return success;
        }

        public IEnumerator<HDCWatcherQueueItem> GetEnumerator()
        {
            lock (this._queueLock)
            {
                foreach (string itemKey in this._queue)
                {
                    yield return this._dictionary[itemKey];
                }
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void CopyTo(Array array, int index)
        {
            CopyTo(array as HDCWatcherQueueItem[], index);
        }

        public int Count
        {
            get { return this._queue.Count; }
        }

        //Examples found don't support this
        public bool IsSynchronized
        {
            get { return false; }
        }

        //Examples found don't support this
        public object SyncRoot
        {
            get { throw new NotSupportedException(); }
        }
    }
}
